﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;

namespace Homework5
{
    public partial class Form2 : Form
    {
        private IFeatureClass pFeaClass;
        public Form2()
        {
            InitializeComponent();
        }
        //带参的构造函数传递数据
        public Form2(IFeatureClass pFClass)
        {
            this.pFeaClass = pFClass;
            
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            IFields pFields = pFeaClass.Fields;
            for (int i = 0; i < pFields.FieldCount;i++ )
            {
                dataGridView1.Columns.Add(pFields.Field[i].Name, pFields.Field[i].AliasName);
            }
            IFeatureCursor pFeaCursor = pFeaClass.Search(null, true);
            IFeature pFeature=pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                int index= dataGridView1.Rows.Add();
                for (int i = 0; i < pFields.FieldCount; i++)
                {
                    dataGridView1.Rows[index].Cells[i].Value = pFeature.get_Value(i);
                    //不运行改写的字段设为只读
                    //int size = dataGridView1.RowCount;
                    //if (!pFeature.Fields.Field[i].Editable)
                    //{
                    //    dataGridView1[index, i].ReadOnly = true;
                    //}
                }
                pFeature=pFeaCursor.NextFeature();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFields pFields = pFeaClass.Fields;
            IFeatureCursor pFeaCursor = pFeaClass.Search(null, true);
            IFeature pFeature = pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                int index = 0;
                int name = (int)pFeature.get_Value(0);
                for (int i = 0; i < pFields.FieldCount; i++)
                {
                    //如果该属性允许改写
                    if (pFeature.Fields.Field[i].Editable )
                    {
                        pFeature.set_Value(i, dataGridView1.Rows[index].Cells[i].Value);
                    }
                }
                pFeature = pFeaCursor.NextFeature();
                index++;
            }
            MessageBox.Show("修改成功");
            //Form2.ActiveForm.Close();
        }
        
    }
}
